import { login, logout, getInfo } from "@/api/user";
import { getToken, setToken, removeToken } from "@/utils/auth";
import { resetRouter } from "@/router";
import { sendJSON } from "@/utils/request";
import { Message } from "element-ui";

const getDefaultState = () => {
  return {
    token: getToken(),
    username: "",
    nickname: "",
    avatar: "",
    uid: "",
    email: "",
    phoneNumber: "",
    sex: "",
    remark: "",
    roleName: ""
  };
};

const state = getDefaultState();

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState());
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_USERNAME: (state, username) => {
    state.username = username;
  },
  SET_NICKNAME: (state, nickname) => {
    state.nickname = nickname;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  SET_UID: (state, uid) => {
    state.uid = uid;
  },
  SET_EMAIL: (state, email) => {
    state.email = email;
  },
  SET_PHONENUMBER: (state, phoneNumber) => {
    state.phoneNumber = phoneNumber;
  },
  SET_SEX: (state, sex) => {
    state.sex = sex;
  },
  SET_REMARK: (state, remark) => {
    state.remark = remark;
  },
  SET_ROLENAME: (state, roleName) => {
    state.roleName = roleName;
  }
};

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, vCore } = userInfo;
    return new Promise((resolve, reject) => {
      sendJSON("/sys/auth/login", {
        username: username.trim(),
        password,
        vCore
      })
        .then(res => {
          Message(res.msg);
          if (!res.success) {
            reject(res);
          }
          const { data } = res;
          data.ACCESS_TOKEN = `Bearer ${data.ACCESS_TOKEN}`;
          commit("SET_TOKEN", data.ACCESS_TOKEN);
          commit("SET_UID", data.uid);
          setToken(data.ACCESS_TOKEN);
          resolve();
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      sendJSON("sys/user/getMsf")
        .then(res => {
          console.log("请求个人信息~", res.data);
          const { data } = res;
          if (!data) {
            return reject("验证失败，请重新登录.");
          }
          let {
            username,
            nickname,
            avatar,
            email,
            phoneNumber,
            sex,
            remark,
            roleName
          } = data;
          console.log("值为", nickname);
          commit("SET_USERNAME", username);
          commit("SET_NICKNAME", nickname);
          commit("SET_AVATAR", avatar);
          commit("SET_EMAIL", email);
          commit("SET_PHONENUMBER", phoneNumber);
          commit("SET_SEX", sex);
          commit("SET_REMARK", remark);
          commit("SET_ROLENAME", roleName);
          resolve(data);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      sendJSON("/sys/auth/logout")
        .then(() => {
          removeToken(); // must remove  token  first
          resetRouter();
          commit("RESET_STATE");
          resolve();
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken(); // must remove  token  first
      commit("RESET_STATE");
      resolve();
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
